import { createRouter, createWebHistory } from 'vue-router';

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/view/login.vue'),
    // 模版
    meta: {
      title: '登录',
      icon: 'el-icon-s-home',
    },
  },

  {
    path: '/',
    name: 'Index',
    component: () => import('@/view/Layout.vue'),
    // 重定向
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('@/view/home.vue'),
      },
      {
        path: '/ai',
        name: 'ai',
        component: () => import('@/view/ai.vue'),
      },
      // 微应用容器
      {
        path: '/:pathMatch(.*)*',
        name: 'MircroAppContent',
        component: () => import('@/components/MircroAppContent'),
      },
    ],
    // layout
  },
];
const router = createRouter({
  history: createWebHistory('/'),
  routes,
});

/**
 * 路由守卫
 * @param {*} to 目标路径
 * @param {*} from 来源路径
 * @param {*} next 继续
 */
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  next();
});

export default router;
